package com.xl98.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xl98.entity.*;
import com.xl98.service.RoleService;
import com.xl98.service.UserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ls
 * @since 2020-05-15
 */
@RestController
@RequestMapping("/api/role")
@Api(value = "角色",tags = {"角色"})
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private UserRoleService userRoleService;

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/all", method = RequestMethod.POST)
    @ApiOperation(value = "所有角色类型")
    public Result selectAllRole () {
        List<Role> roles = roleService.selectAllRole();
        if(StringUtils.checkValNotNull(roles)){
            return ResultFactory.buidResult(ResponseCode.SUCCESS_CODE,ResponseCode.SUCCESS_FIND,roles);
        }
        else return ResultFactory.buildFailResult(ResponseCode.FAIL_FIND);
    }
    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/userRoles", method = RequestMethod.POST)
    @ApiOperation(value = "获取用户所有角色")
    public Result selectRoleById (String userId) {
        List<Role> roles = roleService.selectRoleByUserId(userId);
        if(StringUtils.checkValNotNull(roles)){
            return ResultFactory.buidResult(ResponseCode.SUCCESS_CODE,ResponseCode.SUCCESS_FIND,roles);
        }
        else return ResultFactory.buildFailResult(ResponseCode.FAIL_FIND);
    }

       @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/addUserRole", method = RequestMethod.POST)
    @ApiOperation(value = "为用户添加一个角色类型")
    public Result addUserRole(String userId, String roleId) {
        List<Role> roles = roleService.selectRoleByUserId(userId);
        for (Role role : roles) {
            if (role.getId().equals(roleId)) {
                return ResultFactory.buildFailResult(ResponseCode.FAIL_INSERT);
            }
        }
        UserRole userRole = new UserRole();
        userRole.setUserId(userId);
        userRole.setRoleId(roleId);
        int rs = userRoleService.addUserRole(userRole);
        if (rs > 0) {
            return ResultFactory.buildSuccessResult(ResponseCode.SUCCESS_INSERT);
        } else return ResultFactory.buildFailResult(ResponseCode.FAIL_INSERT);
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/deleteUserRole", method = RequestMethod.POST)
    @ApiOperation(value = "为用户删除一个角色类型")
    public Result deleteUserRole(@RequestBody UserRole userRole) {
        return ResultFactory.buildSuccessResult(userRoleService.deleteUserRole(userRole));
    }

}

